package com.haier.dreambox.demo.net;

import android.util.Log;
import com.haier.dreambox.demo.utils.MyPrefs_;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * Created by smalant on 14/12/29.
 */
@EBean
public class CommonLogInterceptor implements ClientHttpRequestInterceptor {


    private static final String TAG = "RequestInterceptor";

    @Pref
    MyPrefs_ sharePref;

    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {

        httpRequest.getHeaders().set("appId", "MB-STARAC-0000");
        httpRequest.getHeaders().set("appKey", "79e8e252882ef0fe4c91db2349321323");
        httpRequest.getHeaders().set("appVersion", "02.01.14.06251");
        httpRequest.getHeaders().set("clientId", "863985024753285-68dfdd434ece");
        httpRequest.getHeaders().set("accessToken", sharePref.accessToken().get());
        httpRequest.getHeaders().set("Content-Type", "application/json");
        httpRequest.getHeaders().set("Content-Encoding", "utf-8");

        Log.i(TAG, "HTTP-REQUEST-BODY: "+ new String(bytes,"utf-8") + " URI("+httpRequest.getURI()+")");

        return clientHttpRequestExecution.execute(httpRequest, bytes);
    }

}
